package com.eliza.kotlin.design.creation.prototype.test1

import com.eliza.ktor.all.library.utils.ToolsLog
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

/**
 * @version v1.0
 * @ClassName: CitaionTest
 * @Description: TODO(一句话描述该类的功能)
 * @Author: 黑马程序员
 */
object CitaionTest {
    @Throws(Exception::class)
    @JvmStatic
    fun main(args: Array<String>) {

        //创建张三学生对象
        val stu = Student().apply {
            name = "张三"
        }

        //1，创建原型对象
        val citation = Citation().apply {
            this.stu = stu
        }
        val filePath = "design/src/main/resources/prototype.txt"
        ObjectToFile(citation, filePath)
        val citation1 = ObjectFromFile(filePath)


        val stu1 = citation1.stu
        stu1!!.name = "李四"
        citation.show()
        citation1.show()

        ToolsLog.LogPrintln("${citation == citation1}",5)
    }

    private fun ObjectFromFile(filePath: String): Citation {
        //创建对象输入流对象
        val ois = ObjectInputStream(FileInputStream(filePath))
        //读取对象
        val citation1 = ois.readObject() as Citation
        //释放资源
        ois.close()
        return citation1
    }

    private fun ObjectToFile(citation: Citation, filePath: String) {
        //创建对象输出流对象
        val oos = ObjectOutputStream(FileOutputStream(filePath))
        //写对象
        oos.writeObject(citation)
        //释放资源
        oos.close()
    }
}